<?php

namespace app\admin\controller;

use app\admin\model\Template;

class TemplateController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }
    public function read()
    {
     $param =  $this->request->param();
     $template =  new Template();
     $count = $template->count();
     if (isset($param['page']) &&isset($param['limit'])){
         $template= $template->page($param['page'],$param['limit']);
     }
     if (isset($param['module_id'])){
         $template = $template->where('module_id',$param['module_id']);
     }
         $list = $template->select();
        return ['code' => 0, 'msg' => '数据返回成功', 'data' => $list, 'count' => $count];
    }
    public function add()
    {
        $Template = (new Template());
        $post_data = input('post.');
        $status = $Template->save($post_data);
        $msg ='';
        $code='404';
        if ($status){
            $msg='添加成功';
            $code =0;
        }else{
            $msg='添加失败';
            $code= 404;
        }
        return ['msg' => $msg, 'code' => $code];
    }
    public function upd()
    {
        $Template = (new Template());
        $status = $Template->isUpdate(true)->save(input("post."));
        $msg = '更新成功';
        $code = 0;
        if (!$status) {
            $msg = '更新失败';
            $code = 500;
        }
        return ['msg' => $msg, 'code' => $code];
    }
    public function  del(){
        $result =Template::get(input('post.'))->delete();
        if($result){
            return json(['code'=>0,'msg'=>'删除成功']);
        }else{
            return json(['code'=>500,'msg'=>'删除失败']);
        }
    }
}
